package com.itheima.common.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class POIUtils_BackUp {

    //1. 工作簿  Workbook
    //2. 工作表 Sheet
    //3. 行 row
    //4. 单元格  cell

    public static void read() throws IOException {
        File file = new File("D://ordersetting_template.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheetAt = workbook.getSheetAt(0);
        //最后有数据的一行
        int lastRowNum = sheetAt.getLastRowNum();
        //最先有数据的一行
        int firstRowNum = sheetAt.getFirstRowNum();
        System.out.println("first row:"+firstRowNum+",lastRowNum:"+lastRowNum);
        for (int i = firstRowNum;i<=lastRowNum;i++){
            Row row = sheetAt.getRow(i);
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();
            System.out.println("firstCellNum:"+firstCellNum+",lastCellNum:"+lastCellNum);
            for (short j = firstCellNum;j<lastCellNum;j++){
                Cell cell = row.getCell(j);
                System.out.println("单元格的类型:"+cell.getCellType());
                if (cell.getCellType() == Cell.CELL_TYPE_STRING){
                    System.out.println("数据："+cell.getStringCellValue());
                }
                String dataFormatString = cell.getCellStyle().getDataFormatString();
                if(dataFormatString.equals("m/d/yy")) {
                    String format = new SimpleDateFormat("YYYY-MM-dd").format(cell.getDateCellValue());
                    System.out.println("数据："+format);
                }else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
                    System.out.println("数据："+cell.getNumericCellValue());
                }

            }
        }

    }

    public static void main(String[] args) throws IOException {
        POIUtils_BackUp.read();
    }

}
